<%@ page contentType="text/html; charset=utf-8"%>
<%@ taglib prefix="s" uri="/struts-tags"%>
<!DOCTYPE html PUBLIC "-//W3C//DTD XHTML 1.0 Transitional//EN" "http://www.w3.org/TR/xhtml1/DTD/xhtml1-transitional.dtd">
<html xmlns="http://www.w3.org/1999/xhtml">
	<head>
		<meta http-equiv="Content-Type" content="text/html; charset=UTF-8" />
		<title></title>
		<link rel="stylesheet" type="text/css" href="${contextPath}/resource/css/default.css" />
		<link rel="stylesheet" type="text/css" href="${contextPath}/resource/tree/zTreeStyle/zTreeStyle.css"  />
  		<script type="text/javascript" src="${contextPath}/resource/tree/jquery-1.4.2.js"></script>
 		<script type="text/javascript" src="${contextPath}/resource/tree/jquery.ztree-2.6.js"></script>
		<script language="JavaScript">
				var buttens =${jsonButtens};
				var menuButten = ${jsonMenuKey};
				var treeDb =${jsonTree};
				var crustMenuId = null;
				var zTree1;
				var setting;
						setting = {
								isSimpleData: true,
								treeNodeKey: "id",
								treeNodeParentKey: "pId",
								
								checkable: true,
								checkType: {"Y":"ps", "N":"s"},
								callback: {
									change:	zTreeOnChange,
									click: buttenClick 
								}
							};
						$(document).ready(function(){
							refreshTree();
						});
						
				function buttenClick(event, treeId, treeNode){
					var butgrp = treeNode.buttonGrp;
					var buts = stringUtl(butgrp);
					var innhtml="";
					//读取上次checkBox选取的值 存入 menuButten
				    setMenuButt();
					if(buts == null || buts.length<1){
						document.getElementById("grpbuttenCheck").innerHTML="";
						return;
					}
					var checkBut = menuButten[treeNode.value];
					var oldCheck = stringUtl(checkBut);
					for(var i=0;i<buttens.length;i++){
						var butten = buttens[i];
						var values = buttens[i].value;
						if(buts[values] != null && buts[values] != ""){
							innhtml+="<ul><input type='checkbox' ";
							if(buttens[i].value == oldCheck[buttens[i].value]){
								innhtml += "checked='true'";
							}
							innhtml+=" name='grpValues' value='"+buttens[i].value+"' id='grpValues-"+i+
								"'/><label for='grpValues-"+i+"' class='checkboxLabel'>&nbsp;&nbsp;"+buttens[i].name+"</label></ul>";
						}
					}
					crustMenuId = treeNode.value;
					document.getElementById("grpbuttenCheck").innerHTML = innhtml;
				}
				//改为生成Map格式,方便取得数据
				function stringUtl(butgrp){
					var butgrpVales={"":""};
					if(butgrp != null && butgrp != "" ){
						var butts =butgrp.split("");
						for(var i=0;i<butts.length;i++){
							var flg = butts[i];
							if(flg=="1"){
								var sval ="1";
								for(var j=butts.length-i;j>1;j--){
									sval = sval+"0";
								}
								butgrpVales[sval]=sval;
							}
						}
					}
					return butgrpVales;
				}
				function zTreeOnChange(event, treeId, treeNode) {
					getCheckedNodesLength();
				}
				function refreshTree() {
					zTree1 = $("#radioAgencyId").zTree(setting, treeDb);
					getCheckedNodesLength();
				}
				function getCheckedNodesLength() {
					var tmp = zTree1.getCheckedNodes();
					var rolegroupRoleids = "";
					for (var i=0; i<tmp.length; i++) {
						if(i == tmp.length-1){
							rolegroupRoleids += tmp[i].value;
						}else{
							rolegroupRoleids += tmp[i].value+",";
						}
						if(document.getElementById("menuGrpValues").value!=""){
							if(menuButten[tmp[i].value]==null || menuButten[tmp[i].value]==""){
								menuButten[tmp[i].value] = "1111111111";
							}
						}
					}
					 document.getElementById("menuGrpValues").value =rolegroupRoleids;
				}
				function setMenuButt() {
					var boxes = document.getElementsByName("grpValues");
					var checkbutvl = 0;
					for (var i = 0; i < boxes.length; i++){
				        if (boxes[i].checked){
				        	checkbutvl += parseInt(boxes[i].value);
				        }
				    }
					if(crustMenuId != null){
						if(checkbutvl>0){
							menuButten[crustMenuId] = checkbutvl+"";
						}else{
							menuButten[crustMenuId] = "";
						}
						crustMenuId = null;
					}
				}
				function isSubmit(){
					setMenuButt();
					document.getElementById("menuButValues").value =JsonToStr(menuButten);
				}
				//将json对象转换成字符串 
				function JsonToStr(o) {
					var arr = [];
					var fmt = function(s) {
						if (typeof s == 'object' && s != null) 
							return JsonToStr(s);
						return /^(string|number)$/.test(typeof s) ? "\"" + s + "\"" : s;
					}
					for (var i in o) 
						arr.push("\"" + i + "\":" + fmt(o[i]));
					return '{' + arr.join(',') + '}';
				}
		</script>
	</head>
	
	<body >
	   <s:form action="roleAction_updateGrp.do" method="post" namespace="/" onsubmit="isSubmit()"><div><br /> 
			</div><table class="menu_table">
				<tr>
					<th align="center">
						<a href="rolegroupAction_list.do">角色管理</a>&gt;&gt;权限分配
					</th>
				</tr>
			</table>
		<div class="listtable">
<div class="secondDiv">
			<table class="form_table">
				<s:hidden name="role.rlId"></s:hidden>
				<s:hidden name="menuGrpValues" id="menuGrpValues"></s:hidden>
				<s:hidden name="menuButValues" id="menuButValues"></s:hidden>
				
				<tr>
					<td width="50%">
					    <div class="zTreeDemoBackground">
							<ul id="radioAgencyId" class="tree"></ul>
						</div>
					</td>
					<td width="50%" valign="top">
					<div  class="bottomNav">
						<div><font size="3"><strong>&nbsp;&nbsp;&nbsp;&nbsp;按钮权限分配</strong></font><br /></div>
					    <div id="grpbuttenCheck">
						</div>
					</div>
					</td>
				</tr>
			</table>
		<table width="50%" border="0" align="center" cellpadding="3" cellspacing="1">
			<s:token></s:token>
		    <tr>
		      <td align="center"  >
		      <div align="center">
		        <input type="submit" id="subbut" value="" class="apply_style" align="middle" />
		        <input type="button" onclick="javascript:location='roleAction_list.do'" class="back_style"  />
		       </div>
		       </td>
		    </tr>
		</table>
		</div>
		</div>
		</s:form>
	</body>
</html>
